package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.DanmoGalleryWorkBids;
import com.muyu.danmo.domain.DanmoGalleryWorks;
import com.muyu.danmo.mapper.DanmoGalleryWorkBidsMapper;
import com.muyu.danmo.mapper.DanmoGalleryWorksMapper;
import com.muyu.danmo.service.IDanmoGalleryWorkBidsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 画廊出价Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-23
 */
@Service
public class DanmoGalleryWorkBidsServiceImpl implements IDanmoGalleryWorkBidsService
{
    @Autowired
    private DanmoGalleryWorkBidsMapper danmoGalleryWorkBidsMapper;

    /**
     * 查询画廊出价
     *
     * @param id 画廊出价主键
     * @return 画廊出价
     */
    @Override
    public DanmoGalleryWorkBids selectDanmoGalleryWorkBidsById(Long id)
    {
        return danmoGalleryWorkBidsMapper.selectDanmoGalleryWorkBidsById(id);
    }

    /**
     * 查询画廊出价列表
     *
     * @param danmoGalleryWorkBids 画廊出价
     * @return 画廊出价
     */
    @Override
    public List<DanmoGalleryWorkBids> selectDanmoGalleryWorkBidsList(DanmoGalleryWorkBids danmoGalleryWorkBids)
    {
        return danmoGalleryWorkBidsMapper.selectDanmoGalleryWorkBidsList(danmoGalleryWorkBids);
    }

    /**
     * 新增画廊出价
     *
     * @param danmoGalleryWorkBids 画廊出价
     * @return 结果
     */
//    @Transactional
//    @Override
//    public Result<String> insertDanmoGalleryWorkBids(DanmoGalleryWorkBids danmoGalleryWorkBids) {
//        try {
//            DanmoGalleryWorkBids currentBid = danmoGalleryWorkBidsMapper.selectDanmoGalleryWorkBidsById(danmoGalleryWorkBids.getWorksId());
//
//            DanmoGalleryWorks danmoGalleryWorks= danmoGalleryWorkBidsMapper.selectDanmoGalleryWork(currentBid.getWorksId());
//
//            // 检查是否为代理出价
//            if ("1".equals(danmoGalleryWorkBids.getIsProxy())) {
//                // 如果是代理出价，检查是否有更高的出价
//                while (currentBid != null && danmoGalleryWorkBids.getBidAmount().compareTo(currentBid.getBidAmount()) <= 0) {
//                    // 如果当前出价小于或等于代理出价的最高价，则增加出价
//                    if (danmoGalleryWorkBids.getBidAmount().compareTo(danmoGalleryWorkBids.getMaximumPrice()) < 0) {
//                        // 假设加价幅度为10
//                        danmoGalleryWorkBids.setBidAmount(currentBid.getBidAmount() + danmoGalleryWorks.getIncrement());
//                    } else {
//                        // 如果达到最高价，则不再加价
//                        break;
//                    }
//                }
//            } else {
//                // 如果不是代理出价，直接检查出价是否有效
//                if (currentBid != null && danmoGalleryWorkBids.getBidAmount().compareTo(currentBid.getBidAmount()) <= 0) {
//                    return Result.error("出价金额不能低于当前出价金额");
//                }
//            }
//
//            // 设置出价时间和创建时间
//            danmoGalleryWorkBids.setBidDate(DateUtils.getNowDate());
//            danmoGalleryWorkBids.setCreateTime(DateUtils.getNowDate());
//
//            // 插入新的出价记录
//            danmoGalleryWorkBidsMapper.insertDanmoGalleryWorkBids(danmoGalleryWorkBids);
//            return Result.success("出价成功");
//        } catch (Exception e) {
//            // 记录异常信息到日志系统
//            // log.error("出价失败", e);
//            return Result.error("出价失败: " + e.getMessage());
//        }
//    }
   @Autowired
   private DanmoGalleryWorksMapper danmoGalleryWorksMapper;

    @Override
    public Result<DanmoGalleryWorks> insertDanmoGalleryWorkBids(DanmoGalleryWorkBids danmoGalleryWorkBids) {
        try {
            // 获取当前最高出价和拍卖商品信息
//            DanmoGalleryWorkBids currentBid = danmoGalleryWorkBidsMapper.selectDanmoGalleryWorkBidsById(danmoGalleryWorkBids.getWorksId());
            DanmoGalleryWorks danmoGalleryWorks = danmoGalleryWorkBidsMapper.selectDanmoGalleryWork(danmoGalleryWorkBids.getWorksId());

            if (danmoGalleryWorks == null) {
                return Result.error("拍卖商品不存在");
            }

            // 检查出价是否有效
            if (!isValidBid( danmoGalleryWorkBids, danmoGalleryWorks)) {
                return Result.error("出价无效，出价未能超过当前最高价");
            }


            // 设置出价时间和创建时间
            danmoGalleryWorkBids.setBidDate(DateUtils.getNowDate());
            danmoGalleryWorkBids.setCreateTime(DateUtils.getNowDate());

            danmoGalleryWorkBids.setBidAmount(danmoGalleryWorkBids.getBidAmount());
            // 插入新的出价记录
            danmoGalleryWorkBidsMapper.insertDanmoGalleryWorkBids(danmoGalleryWorkBids);
            Long worksId = danmoGalleryWorkBids.getWorksId();
            DanmoGalleryWorks danmoGalleryWorks1 = new DanmoGalleryWorks();
            danmoGalleryWorks1.setId(worksId);
            danmoGalleryWorks1.setCurrentPrice(danmoGalleryWorkBids.getBidAmount());
            System.out.println(danmoGalleryWorks.getBidFrequency());
            danmoGalleryWorks1.setBidFrequency(danmoGalleryWorks.getBidFrequency()+1);
            danmoGalleryWorksMapper.updateDanmoGalleryWorks(danmoGalleryWorks1);
            return Result.success(danmoGalleryWorks,"出价成功");
        } catch (DataAccessException dae) {
            // 记录数据访问异常
            return Result.error("出价失败: 数据访问异常");
        } catch (IllegalArgumentException iae) {
            // 记录非法参数异常
            return Result.error("出价失败: 非法参数异常");
        } catch (Exception e) {
            // 记录其他异常
            return Result.error("出价失败: " + e.getMessage());
        }
    }
    private boolean isValidBid( DanmoGalleryWorkBids newBid, DanmoGalleryWorks danmoGalleryWorks) {
        // 检查新的投标金额是否大于最低要求
        return newBid.getBidAmount() > danmoGalleryWorks.getCurrentPrice();
    }

    //            // 检查是否为代理出价
//            if ("1".equals(danmoGalleryWorkBids.getIsProxy())) {
//                // 如果是代理出价，确保用户设置了最高出价
//                if (danmoGalleryWorkBids.getMaximumPrice() != null && danmoGalleryWorkBids.getMaximumPrice() > 0) {
//                    // 代理出价逻辑
//                    Long nextBidAmount = calculateNextBidAmount(currentBid, danmoGalleryWorkBids.getMaximumPrice(), danmoGalleryWorks);
//                    if (nextBidAmount.compareTo(currentBid == null ? 0L : currentBid.getBidAmount()) > 0) {
//                        danmoGalleryWorkBids.setBidAmount(nextBidAmount);
//                    } else {
//                        return Result.error("代理出价未能超过当前最高价");
//                    }
//                } else {
//                    // 如果没有设置最高出价，视为普通出价
//                    danmoGalleryWorkBids.setIsProxy("0");
//                }
//            }
    private Long calculateNextBidAmount(DanmoGalleryWorkBids currentBid, Long maximumPrice, DanmoGalleryWorks danmoGalleryWorks) {
        // 假设这是加价幅度，类型为 Long
        Long increment = danmoGalleryWorks.getIncrement();

        // 如果当前没有出价记录，从0开始
        Long nextBidAmount = (currentBid == null) ? 0L : currentBid.getBidAmount();

        // 确保新的出价金额不超过用户设定的最高价
        if (nextBidAmount == null) {
            // 确保从0开始
            nextBidAmount = 0L;
        } else {
            // 增加出价幅度
            nextBidAmount += increment;
        }

        return nextBidAmount > maximumPrice ? maximumPrice : nextBidAmount;
    }
//    private boolean isValidBid(DanmoGalleryWorkBids currentBid, DanmoGalleryWorkBids newBid) {
//        // 如果当前没有出价记录，新出价应该是大于0的正数
//        if (currentBid == null) {
//            return newBid.getBidAmount() > 0;
//        }
//        // 如果当前有出价记录，新出价应该高于当前最高出价
//        return newBid.getBidAmount() > currentBid.getBidAmount();
//    }

    /**
     * 修改画廊出价
     *
     * @param danmoGalleryWorkBids 画廊出价
     * @return 结果
     */
    @Override
    public int updateDanmoGalleryWorkBids(DanmoGalleryWorkBids danmoGalleryWorkBids)
    {
        danmoGalleryWorkBids.setUpdateTime(DateUtils.getNowDate());
        return danmoGalleryWorkBidsMapper.updateDanmoGalleryWorkBids(danmoGalleryWorkBids);
    }

    /**
     * 批量删除画廊出价
     *
     * @param ids 需要删除的画廊出价主键
     * @return 结果
     */
    @Override
    public int deleteDanmoGalleryWorkBidsByIds(Long[] ids)
    {
        return danmoGalleryWorkBidsMapper.deleteDanmoGalleryWorkBidsByIds(ids);
    }

    /**
     * 删除画廊出价信息
     *
     * @param id 画廊出价主键
     * @return 结果
     */
    @Override
    public int deleteDanmoGalleryWorkBidsById(Long id)
    {
        return danmoGalleryWorkBidsMapper.deleteDanmoGalleryWorkBidsById(id);
    }

    @Override
    public Result autoBids(DanmoGalleryWorkBids danmoGalleryWorkBids) {
        return null;
    }
}
